package com.atguigu.java1;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 *
 */
public class URLTest1 {
    public static void main(String[] args){

        HttpURLConnection ur1Connection = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            URL ur1 = new URL("http://localhost:8080/exemple/beauty.jpg?Uersname=Tom");

            ur1Connection = (HttpURLConnection) ur1.openConnection();

            ur1Connection.connect();

            is = ur1Connection.getInputStream();
            fos = new FileOutputStream("day10\\beauty.jpg");
            byte[] buffer = new byte[20];
            int len;
            while ((len = is.read(buffer)) != -1){
                fos.write(buffer,0,len);
            }

            System.out.println("下载完成！");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (ur1Connection != null) {
                ur1Connection.disconnect();
            }
        }
    }
}
